package yxy.game.pm2.module.union.player;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.ActiveUtil;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;
import yxy.game.pm2.module.union.UnionContribution.ContributeType;
import yxy.game.pm2.module.union.boss.UnionPlayerBoss;
import yxy.game.pm2.module.union.player.tech.UnionPlayerTech;

import java.util.Date;

public class UnionPlayer extends PlayerModule {
    // bean===========================================================================

    private String unionCode;
    private DateTime forbidTime;// 禁止加入联盟的时间
    private DateTime updateTime;

    public String getUnionCode() {
        return unionCode;
    }

    public void setUnionCode(String unionCode) {
        this.unionCode = unionCode;
    }

    public DateTime getForbidTime() {
        return forbidTime;
    }

    public void setForbidTime(DateTime forbidTime) {
        this.forbidTime = forbidTime;
    }

    public DateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(DateTime updateTime) {
        this.updateTime = updateTime;
    }

    // module===========================================================================

    private UnionPlayerContribution contribution;// 捐献
    private UnionPlayerTech tech;// 科技
    private UnionPlayerBoss boss;// 联盟boss

    public UnionPlayerContribution getContribution() {
        if (contribution == null) {
            contribution = UnionPlayerContribution.create();
        }
        return contribution;
    }

    public void setContribution(UnionPlayerContribution contribution) {
        this.contribution = contribution;
    }

    public UnionPlayerTech getTech() {
        return tech;
    }

    public void setTech(UnionPlayerTech tech) {
        this.tech = tech;
    }

    public UnionPlayerBoss getBoss() {
        return boss;
    }

    public void setBoss(UnionPlayerBoss boss) {
        this.boss = boss;
    }

    // func===========================================================================

    public void update() {
        DateTime now = DateTime.now();
        if (getUpdateTime() == null || !new LocalDate(now).equals(new LocalDate(getUpdateTime()))) {
            setUpdateTime(now);
            // 捐献
            getContribution().update();
            // 科技
            getTech().update();
            // Boss
            getBoss().update(getPlayerCode());
            // 联盟战
            this.set();
        }

        if (getTech().getResetTime() == null) {
            getTech().setResetTime(DateTime.now());
            getTech().setResetTimes(0);
            this.set();
            return;
        }

        //如果不是同一月，初始免费次数
        Date resetTime = getTech().getResetTime().toDate();
        Date nowTime = DateTime.now().toDate();
        if (ActiveUtil.getMonthDiff(resetTime, nowTime) != 0) {
            getTech().setResetTime(DateTime.now());
            getTech().setResetTimes(0);
            this.set();
        }

    }

    public void reset() {
        getContribution().reset();
    }

    public boolean isforbiden() {
        if (getForbidTime() == null) {
            return false;
        }
        return getForbidTime().isAfter(DateTime.now());
    }

    public void forbiden() {
        int hours = fomula.退出联盟再次加入新联盟时间.intValue();
        setForbidTime(DateTime.now().plusHours(hours));
    }

    public void check() {
        {
            // 捐献
            boolean contri = getUnionCode() != null && getContribution().getContribType().equals(ContributeType.NONE);
            new TipHandle().updateTip(getPlayerCode(), TipType.联盟_捐献按钮, contri);
        }
        {
            // BOSS
            boolean boss = getUnionCode() != null && (getBoss().times() > 0 // || getBoss().buytimes() > 0
            );
            new TipHandle().updateTip(getPlayerCode(), TipType.联盟_BOSS按钮, boss);
        }
        {
            // 有申请者的红点
            if (getUnionCode() == null) {
                new TipHandle().updateTip(getPlayerCode(), TipType.联盟_有玩家申请, false);// 针对退出联盟或联盟解散的处理
            }
        }
    }

    // redis==========================================================================

    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:UNION", playerCode);
    }

    public void set() {
        try {
            setv(key(getPlayerCode()), this);
            check();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public UnionPlayer get(String playerCode) {
        try {
            UnionPlayer unionPlayer = getv(key(playerCode));
            if (unionPlayer == null) {
                unionPlayer = MySQLCacheDAO.getV(key(playerCode), UnionPlayer.class);
                if (unionPlayer != null) {
                    unionPlayer.set();
                }
            }

            // TODO 需要改成根据等级开启模块
            if (unionPlayer == null) {
                unionPlayer = create(playerCode);
            }
            unionPlayer.update();
            return unionPlayer;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    static public UnionPlayer create(String playerCode) {
        UnionPlayer unionPlayer = new UnionPlayer();
        unionPlayer.setPlayerCode(playerCode);
        // 捐献
        unionPlayer.setContribution(UnionPlayerContribution.create());
        // 科技
        unionPlayer.setTech(UnionPlayerTech.create());
        // BOSS
        unionPlayer.setBoss(UnionPlayerBoss.create());

        // 联盟战

        unionPlayer.set();
        return unionPlayer;
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                UnionPlayer unionPlayer = getv(key(playerCode));
                if (unionPlayer != null) MySQLCacheDAO.setV(key, unionPlayer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

    @Override
    public boolean tryLock() throws InterruptedException {
        return super.tryLock();
    }

    public static RedisLock lock(String playerCode) {
        return getLock(String.format("lock:%s", key(playerCode)));
    }
}
